<?php

require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/appconf.php');

class mapi_itrial_reportComment extends Site {

    function setup() {
        $this->run_modes('list_comment', 'add', 'list_sub_comment');
        $this->start_mode('list_comment');
        parent::setup();
    }

    function cgiapp_prerun() {

        $this->ProductReportsComment = getModel('Model_ProductReportsComment');
        $this->mapi_object = getModel('MAPI_Base');
        $this->Points = getModel('Model_Points');
        parent::cgiapp_prerun();
    }

    function action_list_comment() {

        $page = intval($this->get_q('page', 1, 'int'));
        $rid = intval($this->get_q('rid', 0, 'int'));
        $pagesize = intval($this->get_q('pagesize', 10, 'int'));;
        $a_result = array();

        if ($rid == 0) {
            return $this->mapi_object->to_json(array(), array(), 0, "報告id為空");
        }

        $kvcobj = getCache('mapi_reportsComment_list', array('expire' => KVC_EXPIRE));
        $reportsCommentList = $kvcobj->get('mapi_reportsComment_list' . $rid . "_" . $page . "_" . $pagesize);
        if (!$reportsCommentList) {

            $orderby = "TB1.addtime desc,TB1.id desc";
            $reportsCommentList = $this->ProductReportsComment->getReportsCommentList($rid, $orderby, $page, $pagesize);
            $kvcobj->set('mapi_reportsComment_list' . $rid . "_" . $page . "_" . $pagesize, $reportsCommentList);
        }


        $a_required_field = array('id', 'oid', 'body', 'status', 'topid', 'subComments', 'photo', 'username', 'mid', 'addtime');

        if (!empty($reportsCommentList['result'])) {
            foreach ($reportsCommentList['result'] as $k => $v) {
                $h_row = array();
                foreach ($a_required_field as $field) {
                    $h_row[$field] = $v[$field];
                }
                $h_row['photo_url'] = imore_avatar($h_row['photo']);
                $a_result[] = $h_row;
            }
        }


        return $this->mapi_object->to_json($a_result, $reportsCommentList['pageinfo']);
    }

    function action_list_sub_comment() {
        $topid = intval($this->get_q('topid', 1, 'int'));
        $page = intval($this->get_q('page', 1, 'int'));
        $a_result = array();
        $pagesize = intval($this->get_q('pagesize', 10, 'int'));;

        if ($topid == 0) {
            return $this->mapi_object->to_json(array(), array(), 0, "留言topid為空");
        }

        $kvcobj = getCache('mapi_reportsSubComment_list', array('expire' => KVC_EXPIRE));
        $data = $kvcobj->get('mapi_reportsSubComment_list' . $topid . "_" . $page . "_" . $pagesize);
        if (!$data) {

            $data = $this->ProductReportsComment->getReportsSubCommentList($topid, "TB1.id desc", $page, $pagesize);
            $kvcobj->set('mapi_reportsSubComment_list' . $topid . "_" . $page . "_" . $pagesize, $data);
        }



        $a_required_field = array('id', 'oid', 'body', 'status', 'topid', 'subComments', 'photo', 'username', 'mid', 'addtime');

        if ($data['result']) {
            foreach ($data['result'] as $k => $v) {
                $h_row = array();
                foreach ($a_required_field as $field) {
                    $h_row[$field] = $v[$field];
                }
                $h_row['photo_url'] = imore_avatar($h_row['photo']);
                $a_result[] = $h_row;
            }
        }

        return $this->mapi_object->to_json($a_result, $data['pageinfo']);
    }

    function action_add() {

        $mid = intval($this->get_q('mid', 0, 'int'));
        $rid = intval($this->get_q('rid', 0, 'int'));
        $message = nl2br($this->get_q('message', ''));
        $token = $this->get_q('token');

        if (!$mid || !$token || !$rid || !$message) {
            return $this->mapi_object->to_json(array(), array(), 0, "用戶id或token或報告id或回覆內容為空");
        }

        $attr = array();
        $attr['oid'] = $rid;
        $attr['topid'] = intval($this->get_q('topid', 0, 'int'));
        $attr['mid'] = $mid;
        $attr['body'] = $message;
        $attr['status'] = 1;
        $attr['addtime'] = date("Y-m-d H:i:s");
        $attr['likes'] = 0;
        $attr['unlikes'] = 0;
        $res = db_add('product_reports_comment', $attr);

        if ($res) {
            $_title = db_query_one("product_reports", array('field' => 'title', 'where' => 'id=' . $attr['oid']));
            $this->Points->points_record($mid, "submit_comment", str_replace("[報告名稱]", $_title['result']['title'], SUBMIT_COMMENT_LANG));

            if (empty($attr['topid'])) {
                $res1 = db_query_one('product_reports', array('field' => 'comments', 'where' => ' id=' . $attr['oid']));
                $reps = $res1['result']['comments'] + 1;
                db_update('product_reports', array('comments' => $reps), ' id=' . mysql_escape_string($attr['oid']));
            } else {
                $res1 = db_query_one('product_reports', array('field' => 'comments', 'where' => ' id=' . $attr['oid']));
                $_sum = $res1['result']['subComments'];
                $attr['sum'] = $_sum + 1;
                db_update('product_reports_comment', array('subComments' => $attr['sum']), 'id=' . mysql_escape_string($attr['topid']));
            }
            return $this->mapi_object->to_json(array(), array(), 1, "success");
        } else {
            return $this->mapi_object->to_json(array(), array(), 0, "回覆失敗");
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new mapi_itrial_reportComment();
$webapp->run();
?>